package top.iiiimonster.module.controller.ai;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.StreamingChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/demo/ai")
@Validated
@Slf4j
public class AiController {

    private final ChatClient chatClient;


    @Autowired
    private StreamingChatClient streamingChatClient;

    @Autowired
    public AiController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @PostMapping("/chat")
    public String generateText(@RequestBody String prompt) {
        return chatClient.call(prompt);
    }

    /**
     * 流式处理返回结果
     * @param prompt
     * @return
     */
    @PostMapping("/stream-chat")
    public Flux<String> streamChat(@RequestParam(value = "prompt") String prompt) {
        return streamingChatClient.stream(prompt);
    }

}
